<template>
  <div class='app-container'>
    <div class='policynumber'>
      <div class='policynumber-tab'>
        <div class='tab-con'>
        	<span @click='toone'>按保单号登记</span>
          <span @click='toPolicynumber'>批量登记</span>
          <span class='active'>Excel表格导入</span>
          <span @click='toPolicyhistory'>保单导入历史</span>
        </div>
      </div>
      <!--  <div class='layout clearfix'>
            <div class='biaoge clearfix'>
              <div class='file'>
                <span class='daoru'>导入文件：</span>
              </div>
              <div class='model'>
                <el-upload class="upload-demo lead" action="https://te.zhibaoke.com/api/services/UploadFile?templateCode=0"  :on-success="loadSuc"  :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="1" :on-exceed="handleExceed" :file-list="fileList" :before-upload="beforeAvatarUpload" accept="excel/xls,excel/xlsx">
                  <div slot="tip" class="el-upload__tip">只能上传.xlsx/.xls文件</div>
                  <el-button size="small" :on-error='error'>
                    <i class='el-icon-upload'></i> 点击上传</el-button>
                </el-upload>
              </div>
            </div> -->
      <div class='layout clearfix'>
        <div class='biaoge clearfix'>
          <el-form :model="ruleForm" :rules="rules2" ref="ruleForm" class="demo-ruleForm" label-width='120px'>
            <el-form-item label="导入文件" prop="filePath">
              <el-input type="hidden" v-model="ruleForm.filePath" ref="getblur" style="display:none"></el-input>
              <el-upload class="upload-demo lead" :action="url" :on-success="loadSuc" :on-exceed="handleExceed" multiple :before-upload="beforeAvatarUpload" accept="excel/xls,excel/xlsx" :on-remove='removeFile' :headers="headers" :limit="1">
                <div slot="tip" class="el-upload__tip" :on-exceed="handleExceed">只能上传xlsx、xls文件</div>
                <el-button size="mini">
                  <i class='el-icon-upload'></i> 点击上传</el-button>
              </el-upload>
              <div class='errorColor'>{{errormessage}}</div>
            </el-form-item>
          </el-form>
        </div>
        <div class='notice2'>
          <div>
            <span class='circle1'>注意事项</span>
          </div>
          <div class='notice3'>
            <span class='circle'></span>如果还没有模板，请
            <a href="/templates/保单导入模板.xlsx">下载模板</a>
          </div>
          <div class='notice3'>
            <span class='circle'></span>请确保表格中的工号与保单号一一对应正确，否则无法导入；</div>
          <div class='notice3'>
            <span class='circle'></span>请确保表格中没有空行，否则空行一下保单号无法导入；</div>
          <div class='notice3'>
            <span class='circle'></span>支持扩展名xlsx、xls</div>
        </div>
      </div>
    </div>
    <div class='btn2'>
      <el-button v-on:click="submit('ruleForm')" class='el-button el-button--primary el-button--small' :loading="sucloading">确定</el-button>
      <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center :close-on-click-modal='false'>
        <div class='tableLead'>excel表格导入失败</br><a :href="filepath" class='links'>下载表格</a>查看失败原因</div>
        <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false" size='mini'>取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false" size='mini'>确 定</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
  </div>
  </div>
</template>
<script>
import { getToken, removeToken } from '@/utils/auth'
export default {
  data() {
    return {
      url: process.env.BASE_API + 'services/UploadFile?templateCode=2',
      ruleForm: {},
      dd: '',
      filepath: null,
      centerDialogVisible: false,
      sucloading: false,
      errormessage: '',
      rules2: {
        filePath: [
          { required: true, message: '请上传文件', trigger: 'blur' },
        ],
      }
    }
  },
  computed: {
    headers() {
      return {
        'Authorization': 'Bearer ' + getToken()
      }
    }
  },
  methods: {
  	toone(){
	  		this.$router.push({
	        name: "policyNumber"
	      });
	  	},
    toPolicynumber() {
      this.$router.push({
        name: 'insentry1',
      })
    },
    toPolicyhistory() {
      this.$router.push({
        name: 'policyhistory'
      })
    },
    loadSuc(val) {
      console.log(val);
      if (val.state == 1) {
        // this.$message.error(val.errorMessage);
        this.errormessage = val.errorMessage
      } else {
        this.dd = val.filePath;
        this.ruleForm.filePath = val.filePath;
        this.$refs.getblur.focus()
        this.$refs.getblur.blur()
        this.$notify({
          title: '成功',
          message: '文件上传成功！',
          type: 'success',

        });
      }
    },
    removeFile() {
      this.ruleForm.filePath = ""
      this.errormessage = ''
    },
    loading() {
      let params = {
        filePath: this.dd
      };
      this.post(
        params,
        "services/app/guaranteeSlip/ImportGuaranteeFromNewExcel"
      ).then(rs => {
        console.log(rs);
        if (rs.success) {
            let id = rs.result.batchId;
            let totalcount=rs.result.totalCount
          if (rs.result.state == 0) {
            this.sucloading = false;
            this.$router.push({ name: "lead", params: { id: id,totalcount:totalcount} });
          } else if (rs.result.state == 1) {
            this.sucloading = false;
            this.centerDialogVisible = true;
            this.filepath = rs.result.errFilePath
          }
        }else{
          console.log(11111)
          this.sucloading=false;
        }
      });
    },
    submit(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          this.sucloading = true;
          this.loading()
        } else {
          return false;
        }
      });

    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制上传1个文件`);
    },
    beforeAvatarUpload(file) {
      let testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isexcel = testmsg === "xls";
      const excel = testmsg === "xlsx";
      console.log(file);
      // const isexcel = file.type === 'excel/xlsx';
      //  const excel = file.type === 'excel/xls';
      if (!isexcel && !excel) {
        // this.excelFileName.push(file.name);
        this.$confirm("上传文件暂时只支持xlsx和xls格式", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          callback: action => {
            // this.$refs.form.resetFields();

            this.$emit("closeEditUpload");
          }
        });
      }
      return isexcel || excel;
    }

  }
}

</script>
<style lang='scss' scoped>
.policynumber {
  .policynumber-tab {
    width: 100%;
    .tab-con {
      border-bottom: 1px solid #cbcbcb;
      span {
        width: 150px;
        line-height: 60px;
        height: 60px;
        display: inline-block;
        text-align: center;
        font-size: 16px;
        cursor: pointer;
        margin-bottom: -1px;
      }
      span:first-child {
        margin-left: 20px;
      }
      .active {
        color: #ee6000;
        background: url(../../assets/images/bomactive2x.png) center 51px no-repeat;
      }
    }
  }
  .layout {
    margin-top: 30px;
    .biaoge {
      float: left;
      width: 520px;
      margin-left: 49px;
      .errorColor {
        color: red;
      }
      .file {
        float: left;
      }
    }
  }

  .model {
    margin-left: 30px;
    float: left;

    clear: right;
    position: relative;
    line-height: 2;
    .el-upload-list__item-name {
      padding-left: 0;
    }
  }

  .notice2 {
    margin-left: 100px;
    float: left;
    color: #555;
    font-size: 13px;
    .notice3 {
      margin-top: 20px;
      a {
        color: #027dca;
      }
    }
  }
  .circle {
    display: inline-block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background-color: #cf1322;
    margin-right: 10px;
  }
  .clearfix:after {
    content: "";
    display: block;
    clear: both;
  }
}

.btn2 {
  width: 100%;
  text-align: center;
  position: absolute;
  bottom: 37px;
  .tableLead {
    width: 150px;
    margin: 0 auto;
  }
}

</style>
